#import <appkit/appkit.h>
#import <video/NXLiveVideoView.h>
#import "MyApp.h"

@implementation MyApp

- appDidInit:sender
{
  NXRect winRect;
  int type;
  NXSize vidSize;
  
  [nretWindow getFrame:&winRect];
  [nretWindow setBackgroundGray:NX_WHITE];
  [window setBecomeKeyOnlyIfNeeded:YES];
// Get the screen size.
  if(![NXLiveVideoView doesWindowSupportVideo:bufWindow 
                   standard:&type size:&vidSize])
  {
    NXRunAlertPanel("No Video Present","This machine is not capable of running video applications. Since this program is exclusively for Video, It will now exit","OK",NULL,NULL);
    [self terminate:self];
  }
  [bufWindow sizeWindow:vidSize.width :vidSize.height];
  [bufWindow makeKeyWindow];
  [nretWindow sizeWindow:vidSize.width :vidSize.height];
  [videoView grab];
  buffed=YES;
  mode = NX_FROMINPUT;
  return self;
}

- setMode:sender
{
  switch([[sender selectedCell] tag])
  {
    case 0:
      if(mode==NX_FROMINPUT) break;mode=NX_FROMINPUT;
      [videoView stop:self];
      [videoView setOutputMode:NX_FROMINPUT];
      [outputBox removeFromSuperview];
      [[window contentView] addSubview:inputBox];
      if(!buffed) {[self swapWindows:self];[retainedButton setState:0];}
      break;
    case 1:
      if(mode==NX_FROMVIEW) break;mode=NX_FROMVIEW;
      [videoView stop:self];
      [videoView setOutputMode:NX_FROMVIEW];
      [inputBox removeFromSuperview];
      [[window contentView] addSubview:outputBox];
      break;
  }
  [[window contentView] display];
  [videoView display];
  return self;
}

- setInput:sender
{
  int types[] = {NX_VIDEOIN1,NX_VIDEOIN2,NX_VIDEOIN3};
  
  [videoView selectInput:types[[[sender selectedCell] tag]]];
  return self;
}

- setBrightness:sender
{
  [videoView setInputBrightness:[sender floatValue]];
  return self;
}

- setGamma:sender
{
  [videoView setInputGamma:[sender floatValue]];
  return self;
}

- setOutGamma:sender
{
  [videoView setOutputGamma:[sender floatValue]];
  return self;
}

- setHue:sender
{
  [videoView setInputHue:[sender floatValue]];
  return self;
}

- setSaturation:sender
{
  [videoView setInputSaturation:[sender floatValue]];
  return self;
}

- setSharpness:sender
{
  [videoView setInputSharpness:[sender floatValue]];
  return self;
}

- swapWindows:sender
{
  BOOL videoWasOn = [videoView isVideoActive]; 
  [videoView stop:self];
  if(buffed)
  {
    buffed = NO;
    [videoView removeFromSuperview];
    [[nretWindow contentView] addSubview:videoView];
    [nretWindow makeKeyAndOrderFront:self];
    [nretWindow display];
    [bufWindow close];
  }
  else
  {
    buffed = YES;
    [videoView removeFromSuperview];
    [[bufWindow contentView] addSubview:videoView];
    [videoView display];
    [bufWindow makeKeyAndOrderFront:self];
    [nretWindow close];
  }
  if(videoWasOn) [videoView start:self];
  return self;
}

- openImage:sender
{
  if(mode==NX_FROMINPUT)
  {
    mode=NX_FROMVIEW;
    [videoView stop:self];
    [videoView setOutputMode:NX_FROMVIEW];
    [inputBox removeFromSuperview];
    [[window contentView] addSubview:outputBox];
    [ioPopup setTitle:"Output"];
    [window display];
  }
  [videoView setImage:self];
  return self;
}

@end
